jQ(function(){
	jQ("div.my_clips_menu a.clipmanage").addClass("clipmanageonit");

	jQ("input.hminput").val(0);
	//analyzeUrl();
	jQ("#mainpart_list_content").data("currentpage",0);
	jQ("#clipmanagehead li").click(function(){
		var qb=jQ(this).attr("queryby");
		if(qb=="cache"||qb=="recycle"){
			window.location.href=jQ("#ctxVal").val()+"/myclip/clip_"+qb+".html";
		}else{
			window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html#"+qb;
		}
	});
	var usertype=eval(jQ("#var_usertype").val());
	if(usertype==2){
		jQ("#clipmanagehead li[queryby='share']").hide();
		jQ("#invitecount").hide();
		jQ("#clipmanagehead li.nonequery").css("width","432px");
	}else{
		window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html";
	}
	loadCacheMainpartRecords();
	jQ("#viewopt").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		closeOnEscape:false,
		draggable:false,
		height:200,
		buttons: {
			
			"确定": function() {
				var opt=jQ("#viewopt");
				var triggerfun=opt.data("function");
				opt.trigger(triggerfun);
				opt.removeData("function");
				jQ("#viewopt_p").hide();
				jQ("#viewopt_img").show();
				activeBtnOnDialog("viewopt",false);
				//jQ(this).dialog('close');
			},
			"取消": function() {
				var viewopt=jQ("#viewopt");
				var triggerfun=jQ("#viewopt").data("function");
				viewopt.unbind(triggerfun);
				viewopt.find("input.eventid").val("");
				viewopt.removeData("function");
				jQ(this).dialog('close');
			}
		},
		close: function() {
			activeBtnOnDialog("viewopt",true);
			jQ("#viewopt_p").show();
			jQ("#viewopt_img").hide();
		},
		open:function(){
		}
	});
	/*
	jQ("div.samepage a").click(function(){
		if(jQ(this).hasClass("onit")){
			return;
		}else{
			jQ("div.samepage a.onit").removeClass("onit");
			jQ(this).addClass("onit");
			jQ("#mainpart_list_content").data("currentpage",0);
			loadCacheMainpartRecords();
		}
	});*/
	
});

function analyzeUrl(){
	var currentpage=eval(getParameter("currentpage"));
	if(isNaN(currentpage)||currentpage==null){
		jQ("#mainpart_list_content").data("currentpage",0);
	}else{
		jQ("#mainpart_list_content").data("currentpage",currentpage);
	}
}

var PAGESIZE=15;
function loadCacheMainpartRecords(){
	var currentpage=eval(jQ("#mainpart_list_content").data("currentpage"));
	var queryby="cache";
	jQ.ajax({
		type:"POST",
		url: "manageClipAjax!loadMainpartRecords.action",
		data:{
			queryby:queryby,
			currentpage:currentpage
		},
		success:function(data){
			var alerttotalcount=eval(jQ(data).find("alerttotalcount").text());
			var memototalcount=eval(jQ(data).find("memototalcount").text());
			var invitetotalcount=eval(jQ(data).find("invitetotalcount").text());
			var cachetotalcount=eval(jQ(data).find("cachetotalcount").text());
			var recycletotalcount=eval(jQ(data).find("recycletotalcount").text());
			
			jQ("#memocount").data("count",memototalcount);
			jQ("#invitecount").data("count",invitetotalcount);
			jQ("#alertcount").data("count",alerttotalcount);
			jQ("#cachecount").data("count",cachetotalcount);
			jQ("#recyclecount").data("count",recycletotalcount);
			
			alerttotalcount=alerttotalcount>99?"99+":alerttotalcount;
			memototalcount=memototalcount>99?"99+":memototalcount;
			invitetotalcount=invitetotalcount>99?"99+":invitetotalcount;
			cachetotalcount=cachetotalcount>99?"99+":cachetotalcount;
			recycletotalcount=recycletotalcount>99?"99+":recycletotalcount;
			
			jQ("#memocount>span.count").empty().append(memototalcount);
			jQ("#invitecount>span.count").empty().append(invitetotalcount);
			jQ("#alertcount>span.count").empty().append(alerttotalcount);
			jQ("#cachecount>span.count").empty().append(cachetotalcount);
			jQ("#recyclecount>span.count").empty().append(recycletotalcount);
			
			var currentpage=eval(jQ(data).find("currentpage").text());
			jQ("#mainpart_list_content").data("currentpage",currentpage);
			var totalcount=eval(jQ(data).find("totalcount").text());
			var pagecount=eval(jQ(data).find("pagecount").text());
			jQ("#mainpart_list_content").data("pagecount",pagecount);
			var currentrecordcount=eval(jQ(data).find("currentrecordcount").text());
			if(totalcount>1)
				jQ("b.clippage_currentrecordcount").empty().append(currentpage*PAGESIZE+1+"-"+(currentpage*PAGESIZE+currentrecordcount));
			else
				jQ("b.clippage_currentrecordcount").empty().append(totalcount);
			jQ("b.clippage_totalcount").empty().append(totalcount);
			if(jQ(data).find("ClipEvent").length==0){
				jQ("#mainpart_list_content").empty().append("<tr><td colspan=6 style='font-size: 12px; text-align: center;'>暂无内容</td></tr>");
				return;
			}
			var cliphtml="";
			jQ(data).find("ClipEvent").each(function(){
				var id=eval(jQ(this).attr("id"));//
				var title=jQ(this).find("title").text();//
				var starttime=eval(jQ(this).find("starttime").text());
				var endtime=eval(jQ(this).find("endtime").text());
				var iswholeday=eval(jQ(this).find("iswholeday").text());
				var createtime=eval(jQ(this).find("createtime").text());
				var updatetime=eval(jQ(this).find("updatetime").text());
				cliphtml+="<tr id='"+queryby+"-"+id+"'>";
				cliphtml+="<td class=\"check\"><div class=\"clip_checkbox\"></div></td>";
				cliphtml+="<td class=\"content\">";
				cliphtml+="<div class=\"cache\">";
				cliphtml+="<span class=\"cachetitle\">"+title+"</span>";
				cliphtml+="<span class=\"time\">";
				if(starttime>0){
					cliphtml+="<span class=\"timevalue\">";
					if(iswholeday){
						cliphtml+="<b >开始于</b>";
						cliphtml+="<b class=\"first\">"+formateDetailTime(starttime)+"</b>";
					}else{
						cliphtml+="<b class=\"first\">"+formateDetailTime(starttime)+"</b>";
						cliphtml+="<b class=\"second\">"+formateDetailTime(endtime)+"</b>";
					}
				}else{
					cliphtml+="<b style='font-weight:normal;font-size:12px;line-height:32px;'>暂无</b>";
				}
				cliphtml+="</span>";	
				cliphtml+="</span>";
				cliphtml+="<span class=\"linetime\" style='width: 133px;' title=\"创建于"+formateDetailTime(createtime)+"\"><span>"+formatCreatetime(createtime)+"</span></span>";
				cliphtml+="<span class=\"createtime\" title=\"更新于"+formateDetailTime(updatetime)+"\">"+formatCreatetime(updatetime)+"</span></div></td>";
				cliphtml+="</tr>";
				
			});
			jQ("#mainpart_list_content").empty().append(cliphtml);
			jQ("#mainpart_list_content tr td.check").unbind("click").bind("click",checkLine);
			jQ("#mainpart_list_content tr td.content").unbind("click").bind("click",toAddClipPage);
			initPaging(currentpage,pagecount);
		}
	});
}


function toAddClipPage(){
	var trobj=jQ(this).parent();
	var ids=trobj.attr("id");
	var eventid=ids.split("-")[1];
	window.location.href=jQ("#ctxVal").val()+"/myclip/add_clip.html?eventid="+eventid;
}

function checkLine(){
	var trobj=jQ(this).parent();
	if(jQ(this).find("div").hasClass("check")){
		jQ(this).find("div").removeClass("check");
		trobj.data("check",false);
	}else{
		jQ(this).find("div").addClass("check");
		trobj.data("check",true);
	}
}

function initPaging(currentpage,pagec){
	jQ("#pagingcontent").empty();
	jQ("#pagingcount").empty();
	var pagecount=pagec;
	if(pagecount==1){
		jQ("#pagingcontent").hide();
		jQ("#pagecount").hide();
		return;
	}else{
		var pagemin=currentpage-2;
		var pagemax=currentpage+2;
		if(pagemin<0){
			pagemin=0;
		}
		if(pagemax>pagecount-1){
			pagemax=pagecount-1;
		}
		var html="";
		html+="<a page='first'><span >&lt;&lt;</span></a>";
		for(var i=pagemin;i<=pagemax;i++){
			if(i==currentpage){
				html+="<b ><span>"+(i+1)+"</span></b>";
			}else
				html+="<a page='"+i+"'><span>"+(i+1)+"</span></a>";
		}
		html+="<a page='last'><span >&gt;&gt;</span></a>";
		jQ("#pagingcontent").append(html);
		jQ("#pagingcontent a").unbind("click").bind("click",function(){
			var page=jQ(this).attr("page");
			var currentpage=0;
			if(page=="first"){
				currentpage=0;
			}else if(page=="last"){
				currentpage=pagecount-1;
			}else{
				currentpage=eval(page);
			}
			jQ("#mainpart_list_content").data("currentpage",currentpage);
			loadMainpartRecords();
			//loadAllMemo(currentpage);
		});
		jQ("#pagingcontent").show();
		jQ("#pagingcount").append("共有"+pagecount+"页").show();
	}
}

function sel_allclick(obj){
	var trlength=jQ("#mainpart_list_content tr").length;
	var ison=jQ(obj).hasClass("sel_allon");
	if(trlength==0){
		if(ison){
			jQ("a.sel_all").removeClass("sel_allon");
		}else{
			jQ("a.sel_all").addClass("sel_allon");
		}
	}else{
		if(ison){
			jQ("a.sel_all").removeClass("sel_allon");
			jQ("#mainpart_list_content tr td.check").each(function(){
				if(jQ(this).find("div").hasClass("check")){
					jQ(this).find("div").removeClass("check");
					var trobj=jQ(this).parent();
					trobj.data("check",false);
				}
			});
		}else{
			jQ("a.sel_all").addClass("sel_allon");
			jQ("#mainpart_list_content tr td.check").each(function(){
				if(!jQ(this).find("div").hasClass("check")){
					jQ(this).find("div").addClass("check");
					var trobj=jQ(this).parent();
					trobj.data("check",true);
				}
			});
		}
	}
}

function selected2Delete(){
	var ids="";
	jQ("#mainpart_list_content td.check div.check").each(function(i){
		var trobj=jQ(this).parent().parent();
		var trid=trobj.attr("id");
		var eventid=eval(trid.split("-")[1]);
		if(i==0){
			ids+=eventid;
		}else{
			ids+=","+eventid;
		}
	});
	if(ids.length>0){
		var info="";
		var title="";
			info="您确定删除这些草稿么？";
			title="删除草稿";
		jQ("#viewopt_eventid").val(ids);
		var opt=jQ("#viewopt");
		opt.bind("deleteEvents",deleteEvents);
		opt.data("function","deleteEvents")
		jQ("#viewopt_p").empty().append(info);
		jQ("#viewopt").dialog( "option", "title", title);
		jQ("#viewopt").dialog("open");
		loadCacheMainpartRecords();
	}
	
}

function deleteEvents(){
	var ids=jQ("#viewopt_eventid").val();
	jQ.ajax({
		type: "POST",
		url:"eventAjax!deletePatchCacheEvents.action",
		data:{
			eventids:ids
		},
		success:function(data){
			window.location.reload();
		}
	});
}

function formateDetailTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy-MM-dd HH:mm");
}

function formateTitleTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy年MM月dd日");
}

